package com.example.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.db.Session;
import cn.hutool.http.server.HttpServerRequest;
import com.example.common.Result;
import com.example.dao.StudentInfoDao;
import com.example.entity.Account;
import com.example.entity.AdminInfo;
import com.example.entity.StudentInfo;
import com.example.entity.TeacherInfo;
import com.example.service.AdminIfoService;
import com.example.service.StudentInfoService;
import com.example.service.TeacherInfoService;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping
public class AccountController {
    @Resource
    private AdminIfoService adminIfoService;

    @Resource
    private TeacherInfoService teacherInfoService;

    @Resource
    private StudentInfoService studentInfoService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping("/login")
    public Result login(@RequestBody Account user, HttpServletRequest request){

        //校验数据是否有空
        if((ObjectUtil.isEmpty(user.getName())) || ObjectUtil.isEmpty(user.getPassword()) || ObjectUtil.isEmpty(user.getLevel())){
            return Result.error("-1","请完善输入信息");
        }

        //
        Integer level = user.getLevel();
        Account loginUser = new Account();
        if (1 == level){
            //管理员
            loginUser = adminIfoService.login(user.getName(),user.getPassword());
        }
        if(2 == level){
            //教师
            loginUser = teacherInfoService.login(user.getName(),user.getPassword());
        }
        if(3 == level){
            //学生
            loginUser = studentInfoService.login(user.getName(),user.getPassword());
        }


        //用session存储信息
        request.getSession().setAttribute("user",loginUser);


        return  Result.success(loginUser);
    }
    @PostMapping("/register")
    public Result register(@RequestBody Account user,HttpServletRequest request){
        if((ObjectUtil.isEmpty(user.getName())) || ObjectUtil.isEmpty(user.getPassword()) || ObjectUtil.isEmpty(user.getLevel())){
            return Result.error("-1","请完善输入信息");
        }
        Integer level = user.getLevel();

        if(2 == level){
            //教师
            TeacherInfo teacherInfo = new TeacherInfo();
            BeanUtils.copyProperties(user,teacherInfo);
            teacherInfoService.add(teacherInfo);
        }
        if(3 == level){
            //学生
            StudentInfo studentInfo = new StudentInfo();
            BeanUtils.copyProperties(user,studentInfo);
            studentInfoService.register(studentInfo);
        }

        return Result.success();
    }

    @GetMapping("/getUser")
    public Result getUser( HttpServletRequest request){
        //先从session中取出数据
        Account user = (Account)request.getSession().getAttribute("user");
        //判断身份
        Integer level=user.getLevel();
        if (1 == level){
            //管理员信息
            AdminInfo adminInfo = adminIfoService.findById(user.getId());

            return Result.success(adminInfo);
        }
        if(2 == level){
            //教师信息
            TeacherInfo teacherInfo = teacherInfoService.findById(user.getId());
            //teacherInfo.setPassword("");//安全考虑
            return Result.success(teacherInfo);
        }
        if(3 == level){
            //学生信息
            StudentInfo studentInfo =studentInfoService.findById(user.getId());
            return Result.success(studentInfo);
        }

        return Result.success(new Account());//返回空账户
    }
    @GetMapping("/getInfo")
    public Result getInfo(){

        StudentInfo studentInfo = studentInfoService.getstatisricsInfo();
        return Result.success(studentInfo);

    }

    @GetMapping("/logout")
    private Result logout(HttpServletRequest request){
        request.getSession().setAttribute("user",null);

        return Result.success();
    }
    @PostMapping("/updatePassword")
    public Result updatePassword(@RequestBody Account account,HttpServletRequest request){
        Account user = (Account)request.getSession().getAttribute("user");
        String oldPassword = account.getPassword();
        if(!user.getPassword().equals(oldPassword)){
            return Result.error("-1","原密码错误");
        }

        String newPassword = account.getNewPassword();

        Integer level = user.getLevel();

        if(1 == level){
            AdminInfo adminInfo = new AdminInfo();
            BeanUtils.copyProperties(user,adminInfo);
            adminInfo.setPassword(newPassword);
            adminIfoService.update(adminInfo);
        }
        else if (2 == level){
            TeacherInfo teacherInfo = new TeacherInfo();
            BeanUtils.copyProperties(user,teacherInfo);
            teacherInfo.setPassword(newPassword);
            teacherInfoService.update(teacherInfo);
        }
        else{
            StudentInfo studentInfo = new StudentInfo();
            BeanUtils.copyProperties(user,studentInfo);
            studentInfo.setPassword(newPassword);
            studentInfoService.update(studentInfo);
        }
        request.getSession().setAttribute("user",null);
        return Result.success();
    }
}
